package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BookOrder {
    static class Book {
        int id;
        String name;
        double price;
        int stock;

        public Book(int id, String name, double price, int stock) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.stock = stock;
        }
    }

    static class OrderItem {
        Book book;
        int quantity;
        double subtotal;

        public OrderItem(Book book, int quantity) {
            this.book = book;
            this.quantity = quantity;
            this.subtotal = book.price * quantity;
        }
    }

    static class Order {
        static int orderCounter = 1;
        int orderId;
        List<OrderItem> items = new ArrayList<>();
        double total;

        public Order() {
            this.orderId = orderCounter++;
        }

        public void addItem(OrderItem item) {
            // 更新库存并添加订单项
            if (item.quantity > item.book.stock) {
                System.out.println("库存不足！当前库存：" + item.book.stock);
                return;
            }
            items.add(item);
            total += item.subtotal;
            item.book.stock -= item.quantity;
        }

        public void printOrder() {
            System.out.println("\n图书订单");
            System.out.println("图书订单号：" + String.format("%05d", orderId));
            System.out.println("图书名称\t购买数量\t图书单价");
            System.out.println("----------------------------------------");
            for (OrderItem item : items) {
                System.out.printf("%-12s%-8d%-12.1f\n",
                        item.book.name, item.quantity, item.book.price);
            }
            System.out.println("----------------------------------------");
            System.out.printf("订单总额：\t%.1f\n\n", total);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 初始化图书数据
        List<Book> books = new ArrayList<>();
        books.add(new Book(1, "Java教程", 30.6, 30));
        books.add(new Book(2, "JSP教程", 42.1, 40));
        books.add(new Book(3, "SSH架构", 47.3, 15));

        Order order = new Order();
        boolean shopping = true;

        while (shopping) {
            // 显示图书列表
            System.out.println("\n图书列表");
            System.out.println("图书编号\t图书名称\t图书单价\t库存数量");
            System.out.println("----------------------------------------");
            for (Book book : books) {
                System.out.printf("%-8d%-12s%-12.1f%-8d\n",
                        book.id, book.name, book.price, book.stock);
            }
            System.out.println("----------------------------------------");

            System.out.print("请输入图书编号选择图书（输入0结束购买）：");
            int bookId = sc.nextInt();

            if (bookId == 0) {
                shopping = false;
                continue;
            }

            Book selected = null;
            for (Book book : books) {
                if (book.id == bookId) {
                    selected = book;
                    break;
                }
            }

            if (selected == null) {
                System.out.println("无效的图书编号！");
                continue;
            }

            System.out.print("请输入购买数量：");
            int quantity = sc.nextInt();

            if (quantity <= 0) {
                System.out.println("购买数量必须大于0！");
                continue;
            }

            order.addItem(new OrderItem(selected, quantity));
            System.out.println("已添加到购物车，请继续购买。");
        }

        order.printOrder();
        sc.close();
    }
}
